<?php
/**
 * 前台登录 退出操作
 *
 *
 *
 *
 * @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */
defined('InShopNC') or exit('Access Invalid!');

class loginControl extends mobileHomeControl {

	public function __construct(){
		parent::__construct();
	}

	/**
	 * 登录
	 */
	public function indexOp(){
        if(empty($_POST['username']) || empty($_POST['password']) || !in_array($_POST['client'], $this->client_type_array)) {
            output_error('登陆失败');
        }

		$model_member = Model('member');

        $array = array();
        $array['member_name']	= $_POST['username'];
        $array['member_passwd']	= md5($_POST['password']);
        $member_info = $model_member->getMemberInfo($array);

        if(!empty($member_info)) {
            $token = createLoginToken($member_info['member_id'], $member_info['member_name'], $_POST['client']);
            if($token) {
                $model_spec=$this->getModelSpec();
                $channel_spec=$model_spec->getGoodsChannel($member_info['member_level']);
                output_data(array('username' => $member_info['member_name'], 'key' => $token,'member_level'=>$member_info['member_level'],'member_channel'=>implode(',',array_keys($channel_spec))));
            } else {
                output_error('登陆失败');
            }
        } else {
            output_error('用户名密码错误');
        }
    }


    /**
     * 授权登录
     * @return  void
     */
    public function oauthLoginTokenOp(){
        $oAuthToken='login_wap_user';
        if(isset($_SESSION[$oAuthToken])){
            $member_info=$_SESSION[$oAuthToken];
            $token = createLoginToken($member_info['member_id'], $member_info['member_name'], 'wap');
            $model_spec=$this->getModelSpec();
            $channel_spec=$model_spec->getGoodsChannel($member_info['member_level']);
            unset($_SESSION[$oAuthToken]);
            echo '<script type="text/javascript" src="'.WAP_SITE_URL.'/js/config.js"></script>';
            echo '<script type="text/javascript" src="'.WAP_SITE_URL.'/js/common.js"></script>';
            echo '<script type="text/javascript">
                    addcookie(\'username\','.var_export($member_info['member_name'],true).');
                    addcookie(\'key\','.var_export($token,true).');
                    addcookie(\'member_level\','.var_export($member_info['member_level'],true).');
                    addcookie(\'member_channel\','.var_export(implode(',',array_keys($channel_spec)),true).');
                    window.location.href=WapSiteUrl+\'/tmpl/member/member.html?act=member\';
                    </script>';
        }else{
            unset($_SESSION[$oAuthToken]);
            header('location: '.WAP_SITE_URL.'/index.php?act=login&op=oauthLoginToken');
        }
    }

    /**
     * 获取规格
     * @return specModel
     */
    public function getModelSpec(){
        return Model('spec');
    }
	/**
	 * 注册
	 */
	public function registerOp(){
		$model_member	= Model('member');

        $register_info = array();
        $register_info['username'] = $_POST['username'];
        $register_info['password'] = $_POST['password'];
        $register_info['password_confirm'] = $_POST['password_confirm'];
        $register_info['email'] = $_POST['email'];
		$register_info['member_tel'] = $_POST['member_tel'];
        $member_info = $model_member->register($register_info);
        if(!isset($member_info['error'])) {
            $token = createLoginToken($member_info['member_id'], $member_info['member_name'], $_POST['client']);
            $model_spec=$this->getModelSpec();
            $channel_spec=$model_spec->getGoodsChannel($member_info['member_level']);
            if($token) {
                output_data(array('username' => $member_info['member_name'], 'key' => $token,'member_level'=>$member_info['member_level'],'member_channel'=>implode(',',array_keys($channel_spec))));
            } else {
                output_error('注册失败');
            }
        } else {
			output_error($member_info['error']);
        }

    }
}
